from .object import Object
from .utils import print_progress_callback

def guess_file_name(track):
  return ', '.join(
    map(lambda a: a.name, track.artists)
  ) + ' - ' + track.title

class Track(Object):
  def set_values(
    title = None,
    artists = None,
    album = None,
    qualities = None,
    raw_data = None
  ):
    if raw_data != None:
      self.set_raw_data(raw_data)
      return
    self.title = title
    self.artists = artists
    self.album = album
    self.qualities = qualities

  def guess_file_name(self):
    return guess_file_name(self)

  def guess_quality(self):
    return self.qualities[len(self.qualities) - 1]

  def stream(self, stream, quality = None, progress_callback = None):
    # implement this!
    raise ".stream isn't implemented!"

  def download(self,
               file_format = '.flac',
               to_file = None,
               quality = None,
               print_progress = False):
    if quality == None:
      quality = self.guess_quality()
    if to_file == None:
      to_file = self.guess_file_name()
    file_name = to_file + file_format

    # download and decrypt track
    with open(file_name, 'wb') as stream:
      self.stream(
        stream, quality = quality,
        progress_callback = print_progress_callback if print_progress else None
      )
